﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Data;
using System.Web.Routing;

namespace VQWEB.common
{
    public static class ReadXML
    {
        private static string path_xml = HttpContext.Current.Server.MapPath("~/administrator/modules.xml");
        public static string MenuAdmin() {
            string menu = "<ul class=\"nav navbar-nav\" id=\"menu_backend\">";
            XDocument xdoc = XDocument.Load(path_xml);
            var query = from p in xdoc.Element("modules").Elements("module")                       
                        select p;
            foreach (XElement element in query)
            {
                if (element.Attribute("name").Value == "admin_files")
                {
                    menu += "<li><a data-toggle=\"modal\" data-target=\"#uploadfiles\" href=" + element.Attribute("url").Value + ">" + element.Attribute("title").Value + "</a></li>";
                }
                else if (element.Elements("module").Count() > 0)
                {
                    var submenu = from s in element.Elements("module")
                                  select s;
                
                        menu += "<li class=\"dropdown\"><a data-toggle=\"dropdown\" class=\"dropdown-toggle\" href=" + element.Attribute("url").Value + ">" + element.Attribute("title").Value + "<b class=\"caret\"></b></a>";
                        menu += " <ul class=\"dropdown-menu\">";
                        foreach (XElement child in submenu)
                        {
                            menu += "<li><a href=" + child.Attribute("url").Value + ">" + child.Attribute("title").Value + "</a></li>";
                        }
                        menu += "</ul>";
                        menu += "</li>";
                    
                }
                else
                {
                    menu += "<li><a href=" + element.Attribute("url").Value + ">" + element.Attribute("title").Value + "</a></li>";
                }
            }
            menu += "</ul>";  
            return menu;
        }

        public static DataTable Modules()
        {
            XDocument xdoc = XDocument.Load(path_xml);
            var query = from m in xdoc.Descendants("module")
                        select new
                      {
                          name = m.Attribute("name").Value,                         
                          title = m.Attribute("title").Value
                      };
            DataTable obj = HelperData.LINQToDataTable(query);
            return obj;
        }

        public static void Routing(RouteCollection routes) {

            XDocument xdoc = XDocument.Load(path_xml);
            var query = from p in xdoc.Element("modules").Descendants()
                        select p;
            foreach (XElement element in query)
            {
                string name = element.Attribute("name").Value.Trim();
                string url = element.Attribute("url").Value.Trim();
                string src = element.Attribute("src").Value.Trim();
                if (url.IndexOf("/") == 0)
                {
                    url = url.Remove(0, 1);
                }
                routes.MapPageRoute(name, url, src);
               
            }

            routes.MapPageRoute("admin_changepassword", "admin/changepassword", "~/administrator/change-pass.aspx");
            routes.MapPageRoute("admin_forgetpassword", "admin/forgetpassword", "~/administrator/forgetpassword.aspx");

        }

    }

 
}